<!-- 课程详情页 -->
<template>
	<view class="course-detail">
		<!-- 顶部栏 -->
		<view class="header-bar">
			<text class="back-icon" @click="goBack">←</text>
			<text class="title">课程详情</text>
		</view>

		<!-- 视频播放器 -->
		<view class="video-section">
			<video-player 
				:videoUrl="currentChapter.url" 
				:title="currentChapter.title"
				:playlist="chapterList"
			></video-player>
		</view>

		<!-- 课程信息 -->
		<view class="course-info">
			<view class="course-title">{{course.courseName}}</view>
			<view class="course-stats">
				<text class="stat-item">{{course.views || 0}}次观看</text>
				<text class="stat-item">{{course.createTime}}</text>
			</view>
			<view class="course-desc">{{course.remark}}</view>
		</view>

		<!-- 课程章节列表 -->
		<view class="chapter-list">
			<view class="chapter-title">课程目录</view>
			<view class="chapter-item" v-for="(chapter, index) in chapterList" :key="index" @click="playChapter(chapter)">
				<view class="chapter-item-left">
					<text class="chapter-index">{{index + 1}}</text>
					<view class="chapter-info">
						<text class="chapter-name">{{chapter.title}}</text>
						<text class="chapter-duration">{{chapter.duration || '00:00'}}</text>
					</view>
				</view>
				<view class="chapter-item-right">
					<text class="play-status" :class="{'active': currentChapter.id === chapter.id}">
						{{currentChapter.id === chapter.id ? '正在播放' : '播放'}}
					</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import VideoPlayer from '../index/components/video-player.vue'
	import request from '@/common/request.js';
	import { Platform } from "@/static/config.js"

	export default {
		components: {
			VideoPlayer
		},
		data() {
			return {
				courseId: '',
				course: {},
				chapterList: [],
				currentChapter: {}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.courseId = options.id;
				this.getCourseDetail();
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			getCourseDetail() {
				request({
					url: Platform.api_requestUrl + '/treatment/kecheng/' + this.courseId,
					method: 'GET'
				}).then(response => {
					if (response.code === 200) {
						this.course = response.data;
						// 处理章节列表
						this.chapterList = response.data.treatmentKechengZhangjieList.map(chapter => ({
							id: chapter.kczjId,
							zhangjiemingcheng: chapter.zhangjiemingcheng,
							shipinlujing: chapter.shipinlujing,
							duration: chapter.duration || '00:00',
							title: chapter.zhangjiemingcheng,
							description: chapter.zhangjiejieshao,
							url: Platform.api_requestUrl + '/common/getVideoUrl?videoPath=' + chapter.shipinlujing
						}));
						
						// 设置第一个章节为当前播放章节
						if (this.chapterList.length > 0) {
							this.currentChapter = this.chapterList[0];
						}
					} else {
						uni.showToast({
							title: '获取课程详情失败',
							icon: 'none'
						});
					}
				}).catch(error => {
					uni.showToast({
						title: '获取课程详情失败',
						icon: 'none'
					});
				});
			},
			playChapter(chapter) {
				this.currentChapter = chapter;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.course-detail {
		min-height: 100vh;
		background: #f5f5f5;
	}

	.header-bar {
		display: flex;
		align-items: center;
		height: 60px;
		background: #f45a1c;
		padding: 0 15px;

		.back-icon {
			color: #fff;
			font-size: 24px;
			margin-right: 15px;
		}

		.title {
			color: #fff;
			font-size: 18px;
			font-weight: bold;
		}
	}

	.video-section {
		width: 100%;
		background: #000;
	}

	.course-info {
		padding: 15px;
		background: #fff;
		margin-bottom: 10px;

		.course-title {
			font-size: 18px;
			font-weight: bold;
			color: #333;
			margin-bottom: 10px;
		}

		.course-stats {
			display: flex;
			gap: 15px;
			margin-bottom: 10px;

			.stat-item {
				font-size: 14px;
				color: #666;
			}
		}

		.course-desc {
			font-size: 14px;
			color: #666;
			line-height: 1.5;
		}
	}

	.chapter-list {
		background: #fff;
		padding: 15px;

		.chapter-title {
			font-size: 16px;
			font-weight: bold;
			color: #333;
			margin-bottom: 15px;
		}

		.chapter-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 12px 0;
			border-bottom: 1px solid #eee;

			&:last-child {
				border-bottom: none;
			}

			.chapter-item-left {
				display: flex;
				align-items: center;
				flex: 1;

				.chapter-index {
					width: 24px;
					height: 24px;
					background: #f45a1c;
					color: #fff;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 12px;
					margin-right: 10px;
				}

				.chapter-info {
					flex: 1;

					.chapter-name {
						font-size: 14px;
						color: #333;
						margin-bottom: 5px;
					}

					.chapter-duration {
						font-size: 12px;
						color: #999;
					}
				}
			}

			.chapter-item-right {
				.play-status {
					font-size: 14px;
					color: #666;
					padding: 5px 10px;
					border: 1px solid #ddd;
					border-radius: 15px;

					&.active {
						color: #f45a1c;
						border-color: #f45a1c;
					}
				}
			}
		}
	}
</style>